<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Groupbooking extends Base
{
    use HasFactory;

    //拼团团长表
    public $table = 'groupbooking';
    public static function GroupbookingFirst($id)
    {
        return self::where('id', $id)->first();
    }

    /**
     * @param $post
     * @return mixed
     * User: 张浩
     * Date: 2022/11/20
     * Time: 19:00
     * 添加拼团数据
     */
    public static function GroupbookingCreate($post)
    {
        return self::create($post);
    }
    /**
     * @param $groupbookinguid
     * @param $groupbookingcomboid
     * @return mixed
     * User: 张浩
     * Date: 2022/11/20
     * Time: 18:59
     * 检测当前用户当前商品是否正在拼团
     */
    public static function GroupbookingDetection($groupbookinguid,$groupbookingcomboid)
    {
        return self::where('groupbooking_uid',$groupbookinguid)
            ->where('groupbooking_combo_id',$groupbookingcomboid)
            ->where('groupbooking_stare',0)
            ->first();
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * User: 张浩
     * Date: 2022/11/16
     * Time: 19:08
     * 关联商品表
     */
    public function StoreGoods()
    {
        return $this->belongsTo(StoreGoods::class, 'groupbooking_combo_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * User: 张浩
     * Date: 2022/11/16
     * Time: 19:15
     * 关联用户表获取团长数据
     */
    public function Renting()
    {
        return $this->belongsTo(Renting::class, 'groupbooking_uid', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * User: 张浩
     * Date: 2022/11/16
     * Time: 20:23
     * 团长信息关联拼团成员表
     */
    public function GroupbookingUser()
    {
        return $this->belongsTo(GroupbookingUser::class, 'id', 'groupbooking_id');
    }

    /**
     * @param $uid
     * @return mixed
     * User: 张浩
     * Date: 2022/11/16
     * Time: 19:03
     * 根据用户id判断当前用户是否有正在拼团的商品
     */
    public static function GroupbookingUidFirst($uid)
    {
        return self::with('StoreGoods','Renting')
            ->where('groupbooking_uid', $uid)
            ->where('groupbooking_stare', 0)
            ->first();
    }

    /**
     * @param $id
     * @return array
     * User: 张浩
     * Date: 2022/11/20
     * Time: 10:21
     * 获取当前商品下所有正在拼团信息
     */
    public static function GroupBookingmessage($id)
    {
        return self::where('groupbooking_stare',0)
            ->where('groupbooking_combo_id',$id)
            ->get()
            ->toArray();
    }
    public function getStatus(){
        return $this->hasMany(OrderTab::class,'id','groupbooking_stare');
    }
}
